package com.east.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.east.dto.ClueDTO;
import com.east.dto.CustomerDTO;
import com.east.entity.Customer;
import com.east.entity.User;
import com.east.manager.CustomerManager;
import com.east.mapper.CustomerMapper;
import com.east.query.CustomerQuery;
import com.east.result.Result;
import com.east.service.CustomerService;
import com.east.utils.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;

    @Override
    public Result save(CustomerDTO customerDTO) {
        Customer customer = BeanUtil.copyProperties(customerDTO, Customer.class);
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        customer.setCreateBy(loginUser.getId());
        customer.setCreateTime(new Date());
        int insert = customerMapper.insert(customer);
        return insert > 0 ? Result.OK() : Result.FAIL("添加失败");
    }

    @Override
    public Result list(Integer currentPage, Integer pageSize, String fullName) {
        CustomerQuery customerQuery = new CustomerQuery();
        customerQuery.setFullName(fullName);
        Page<CustomerDTO> page = PageHelper.startPage(currentPage, pageSize);
        customerMapper.list(customerQuery);
        PageData<CustomerDTO> pageData = new PageData<CustomerDTO>(currentPage, pageSize,
                Integer.valueOf(page.getTotal() + ""), page.getPages(), page.getResult());
        return Result.OK(pageData);
    }
}
